<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/28
  Time: 14:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/pintuer.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
<head>
    <title>教室信息</title>
</head>
<body>
<div>
    <button class="button" onclick="addRoom()">新增教室</button><br>
</div>
<div class="margin">
    <button class="button" onclick="addRoom()">新增教室</button><br>
    <table class="table">
        <thead>
        <tr>
            <td>教室ID</td>
            <td>教室位置</td>
            <td>操作</td>
        </tr>
        </thead>
        <tbody id="tb">

        </tbody>
    </table>
</div>
<%--分页区域--%>
<div class="margin">
    每页显示<select id="pageSizeSelect" onchange="onSelectChange()">
    <option value="2" selected="selected">2</option>
    <option value="5">5</option>
    <option value="10">10</option>
</select>条数据，当前是第<span id="currentPageSpan" onchange="onChange()"></span> 页，
    总共<span id="totalPageSpan" onchange="Allpage()"></span> 页
    <button class="button" onclick="firstPage()">首页</button>
    <button class="button" onclick="prePage()">上一页</button>
    <button class="button" onclick="nextPage()">下一页</button>
    <button class="button" onclick="lastPage()">尾页</button>
</div>
<%--一个编辑用户信息的模态框--%>
<div class="modal" id="editModal">
    <%--编辑模态框--%>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>编辑教室信息</h4>
            </div>
            <div class="modal-body">
                <%--<label class="control-label">教室ID</label>
                <input class="form-control" type="text" id="idInput" placeholder="请输入教室ID">--%>
                <label class="control-label">教室位置</label>
                <input class="form-control" type="text" id="locationInput" placeholder="请输入教室位置">
                <input type="hidden" id="idInput">
            </div>
            <div class="modal-footer">
                <button class="button" data-dismiss="modal">取消</button>
                <button class="button" onclick="editConfirm()">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    //当前页
    var currentPage = 1;
    //每页记录个数
    var pageSize = 2;
    //总共有多少页
    var totalPage ;

    $(function () {
        //loadList();
        loadListByPage();
    });
    //用户修改的每页展示个数
    function onSelectChange() {
        pageSize = $("#pageSizeSelect").val();
        loadListByPage();
        currentPage = 1;
    }
    function Allpage() {
        pageSize = $("#currentPageSpan").val();
       loadListByPage();
    }
    //当前第几页
    function onChange() {
        totalPage = $("#currentPageSpan").val()
    }
    //首页回调方法
    function firstPage() {
        currentPage = 1;
        loadListByPage();
    }
    /*上一页的回调方法*/
    function prePage() {
        currentPage --;
        if (currentPage < 1){
            currentPage = 1;
        }
        loadListByPage();
    }
    //下一页回调方法
    function nextPage() {
        currentPage++;
        if (currentPage > totalPage){
            currentPage = totalPage;
        }
        loadListByPage();
    }
    //尾页回调方法
    function lastPage() {
        currentPage = totalPage;
        loadListByPage();
    }
    function addRoom() {
        showEditWindow();
    }
    function showRoom(roomList) {
        var html ='';
        for (var i=0;i<roomList.length;i++){
            var item = roomList[i];
            console.log(JSON.stringify(item));
            html+="<tr>";
            html+="<td>"+item.id+"</td>";
            html+="<td>"+item.location+"</td>";
            html+="<td><button class='button' onclick='getRoomDetail(\"" + item.id + "\")'>编辑</button>"+
                      "<button class='button' onclick='deleteOneRoom(\"" + item.id + "\")' >删除</button></td>"
            html+"</tr>";
        }
        $("#tb").html(html);
    }
    //添加教室的模态框
    function showEditWindow(room) {
        if(room!=null){
            console.log(JSON.stringify(room));
            $("#idInput").val(room.id);
            $("#locationInput").val(room.location);
        }else {
            //数据全部归零
            $("#idInput").val(0);
            $("#locationInput").val("");
        }
        $("#editModal").modal("show");
    }
    //分页加载用户列表数据
    function loadListByPage() {
        $.ajax({
            url:"${pageContext.request.contextPath}/room/getListBypage" ,
            type:'get',
            data:{"currentPage":currentPage,"pageSize":pageSize},
            dataType:'json',
            success:function (result) {
                if (result.code == 0){
                    showRoom(result.roomList);//分页数据
                    totalPage = result.totalPage;//总共有多少页
                    $("#currentPageSpan").html(currentPage);
                    $("#totalPageSpan").html(totalPage);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    //加载教室数据
    function loadList() {
       $.ajax({
           url:"${pageContext.request.contextPath}/room/getroom",
           type:'get',
           dataType:'json',
           success:function (result) {
               if (result.code == 0){
                   showRoom(result.roomList);
               }
           },
           error:function (err) {
               console.log(JSON.stringify(err))
           }
       });
    }

    //确认按钮
    function editConfirm() {
         var idValue = $("#idInput").val();
         var locationValue = $("#locationInput").val();
         $.ajax({
             url:"${pageContext.request.contextPath}/room/edit",
             type:'post',
             contentType:'application/x-www-form-urlencoded',
             data: {"id":idValue,"location":locationValue},
             dataType:'json',
             success:function (result) {
                 if (result.code == 0){
                     $("#editModal").modal("hide");//添加成功之后将添加模态框隐藏
                     loadListByPage();
                 }else {
                     alert(result.message)
                 }
             },
             error:function (err) {
                  console.log(JSON.stringify(err))
             }
         });
    }
    function deleteOneRoom(id) {
        $.ajax({
            url: "${pageContext.request.contextPath}/room/delete",
            type: 'get',
            contentType:'application/x-www-form-urlencoded',
            data: {"id":id},//传递用户id
            dataType: 'json',
            success:function (result) {
                if (result.code == 0){
                    loadListByPage();
                    alert(result.message);
                }else {
                    alert(result.message);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
    function getRoomDetail(id) {
        $.ajax({
            url: "${pageContext.request.contextPath}/room/getone",
            type: 'get',
            data: {"id":id},//传递用户id
            dataType: 'json',
            success:function (result) {
                if (result.code == 0){
                    //显示用户编辑模态框
                    showEditWindow(result.roomList);
                }else {
                    alert(result.message);
                }
            },
            error:function (err) {
                console.log(JSON.stringify(err))
            }
        });
    }
</script>
</body>
</html>
